package com.guodu.dao.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.MaterialChukuDao;
import com.guodu.entity.MaterialChuku;
import com.guodu.entity.MaterialchukuStatus;
import com.guodu.entity.Units;
import com.guodu.vo.MaterialChukuQueryVo;

@Repository("materialChukuDao")
public class MaterialChukuDaoImpl extends BaseDao implements MaterialChukuDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialChuku> findByVo(MaterialChukuQueryVo materialChukuQueryVo,Units units,
			int curPage, int pageSize) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from MaterialChuku m where 1=1");
		if(materialChukuQueryVo!=null){
			if(materialChukuQueryVo.getDrawingnum()!=null&&materialChukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialChukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialChukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialChukuQueryVo.getFunitsId()+")");
			}
			if(materialChukuQueryVo.getMaterialCode()!=null&&materialChukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialChukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialChukuQueryVo.getMaterialName()!=null&&materialChukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialChukuQueryVo.getMaterialName()+"%'");
			}
			if(materialChukuQueryVo.getCkBeginDate()!=null&&materialChukuQueryVo.getCkBeginDate().trim().length()>0){
				hql.append(" and ckTime >= TO_DATE('"+materialChukuQueryVo.getCkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getCkEndDate()!=null&&materialChukuQueryVo.getCkEndDate().trim().length()>0){
				hql.append(" and ckTime <= TO_DATE('"+materialChukuQueryVo.getCkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getSpecifications()!=null&&materialChukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialChukuQueryVo.getSpecifications()+"%'");
			}
			if(materialChukuQueryVo.getTexture()!=null&&materialChukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialChukuQueryVo.getTexture()+"%'");
			}
			if(materialChukuQueryVo.getCostType()!=null&&materialChukuQueryVo.getCostType().trim().length()>0){
				hql.append(" and costType = '"+materialChukuQueryVo.getCostType()+"'");
			}
			if(materialChukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialChukuQueryVo.getUnitsId());
			}
			if(materialChukuQueryVo.getCreateBeginTime()!=null&&materialChukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialChukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getCreateEndTime()!=null&&materialChukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialChukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getStatus()!=null&&materialChukuQueryVo.getStatus().trim().length()>0&&!materialChukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialChukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		hql.append(" order by ckId desc");
		return super.findPages(hql.toString(), curPage, pageSize);
	}

	@Override
	public int findSizeByVo(
			MaterialChukuQueryVo materialChukuQueryVo,Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select count(*) from MaterialChuku m where 1=1");
		if(materialChukuQueryVo!=null){
			if(materialChukuQueryVo.getDrawingnum()!=null&&materialChukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialChukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialChukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialChukuQueryVo.getFunitsId()+")");
			}
			if(materialChukuQueryVo.getMaterialCode()!=null&&materialChukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialChukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialChukuQueryVo.getMaterialName()!=null&&materialChukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialChukuQueryVo.getMaterialName()+"%'");
			}
			if(materialChukuQueryVo.getCkBeginDate()!=null&&materialChukuQueryVo.getCkBeginDate().trim().length()>0){
				hql.append(" and ckTime >= TO_DATE('"+materialChukuQueryVo.getCkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getCkEndDate()!=null&&materialChukuQueryVo.getCkEndDate().trim().length()>0){
				hql.append(" and ckTime <= TO_DATE('"+materialChukuQueryVo.getCkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getSpecifications()!=null&&materialChukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialChukuQueryVo.getSpecifications()+"%'");
			}
			if(materialChukuQueryVo.getTexture()!=null&&materialChukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialChukuQueryVo.getTexture()+"%'");
			}
			if(materialChukuQueryVo.getCostType()!=null&&materialChukuQueryVo.getCostType().trim().length()>0){
				hql.append(" and costType = '"+materialChukuQueryVo.getCostType()+"'");
			}
			if(materialChukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialChukuQueryVo.getUnitsId());
			}
			if(materialChukuQueryVo.getCreateBeginTime()!=null&&materialChukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialChukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getCreateEndTime()!=null&&materialChukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialChukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getStatus()!=null&&materialChukuQueryVo.getStatus().trim().length()>0&&!materialChukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialChukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		return super.findInt(hql.toString());
	}

	@Override
	public MaterialChuku findById(String id) {
		// TODO Auto-generated method stub
		return (MaterialChuku) super.findById(MaterialChuku.class, id);
	}

	@Override
	public void del(String id) {
		// TODO Auto-generated method stub
		super.delete(MaterialChuku.class, id);
	}

	@Override
	public void delByIds(String[] ids) {
		// TODO Auto-generated method stub
		super.deleteAllById(MaterialChuku.class, ids);
	}

	@Override
	public void save(MaterialChuku entity) {
		// TODO Auto-generated method stub
		entity.setCkId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		super.save(entity);
	}

	@Override
	public void examine(String ids, Long status) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialChuku set status =");
		hql.append(status);
		hql.append(" where ckId in (");
		hql.append(ids);
		hql.append(")");
		super.executeUpdate(hql.toString());
	}

	@Override
	public void update(MaterialChuku entity) {
		// TODO Auto-generated method stub
		super.update(entity);
	}

	@Override
	public void changeStatus(List<MaterialchukuStatus> entities) {
		// TODO Auto-generated method stub
		super.saveAll(entities);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialchukuStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return super.findByProperty(MaterialchukuStatus.class, "materialId", id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialChuku> findAllByVo(
			MaterialChukuQueryVo materialChukuQueryVo, Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from MaterialChuku m where 1=1");
		if(materialChukuQueryVo!=null){
			if(materialChukuQueryVo.getDrawingnum()!=null&&materialChukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialChukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialChukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialChukuQueryVo.getFunitsId()+")");
			}
			if(materialChukuQueryVo.getMaterialCode()!=null&&materialChukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialChukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialChukuQueryVo.getMaterialName()!=null&&materialChukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialChukuQueryVo.getMaterialName()+"%'");
			}
			if(materialChukuQueryVo.getCkBeginDate()!=null&&materialChukuQueryVo.getCkBeginDate().trim().length()>0){
				hql.append(" and ckTime >= TO_DATE('"+materialChukuQueryVo.getCkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getCkEndDate()!=null&&materialChukuQueryVo.getCkEndDate().trim().length()>0){
				hql.append(" and ckTime <= TO_DATE('"+materialChukuQueryVo.getCkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getSpecifications()!=null&&materialChukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialChukuQueryVo.getSpecifications()+"%'");
			}
			if(materialChukuQueryVo.getTexture()!=null&&materialChukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialChukuQueryVo.getTexture()+"%'");
			}
			if(materialChukuQueryVo.getCostType()!=null&&materialChukuQueryVo.getCostType().trim().length()>0){
				hql.append(" and costType = '"+materialChukuQueryVo.getCostType()+"'");
			}
			if(materialChukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialChukuQueryVo.getUnitsId());
			}
			if(materialChukuQueryVo.getCreateBeginTime()!=null&&materialChukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialChukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getCreateEndTime()!=null&&materialChukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialChukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getStatus()!=null&&materialChukuQueryVo.getStatus().trim().length()>0&&!materialChukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialChukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		hql.append("order by createTime desc");
		return super.find(hql.toString());
	}

	@Override
	public BigDecimal findAllMoney(MaterialChukuQueryVo materialChukuQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select sum(money) from MaterialChuku m where 1=1");
		if(materialChukuQueryVo!=null){
			if(materialChukuQueryVo.getDrawingnum()!=null&&materialChukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialChukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialChukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialChukuQueryVo.getFunitsId()+")");
			}
			if(materialChukuQueryVo.getMaterialCode()!=null&&materialChukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialChukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialChukuQueryVo.getMaterialName()!=null&&materialChukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialChukuQueryVo.getMaterialName()+"%'");
			}
			if(materialChukuQueryVo.getCkBeginDate()!=null&&materialChukuQueryVo.getCkBeginDate().trim().length()>0){
				hql.append(" and ckTime >= TO_DATE('"+materialChukuQueryVo.getCkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getCkEndDate()!=null&&materialChukuQueryVo.getCkEndDate().trim().length()>0){
				hql.append(" and ckTime <= TO_DATE('"+materialChukuQueryVo.getCkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getSpecifications()!=null&&materialChukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialChukuQueryVo.getSpecifications()+"%'");
			}
			if(materialChukuQueryVo.getTexture()!=null&&materialChukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialChukuQueryVo.getTexture()+"%'");
			}
			if(materialChukuQueryVo.getCostType()!=null&&materialChukuQueryVo.getCostType().trim().length()>0){
				hql.append(" and costType = '"+materialChukuQueryVo.getCostType()+"'");
			}
			if(materialChukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialChukuQueryVo.getUnitsId());
			}
			if(materialChukuQueryVo.getCreateBeginTime()!=null&&materialChukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialChukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getCreateEndTime()!=null&&materialChukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialChukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getStatus()!=null&&materialChukuQueryVo.getStatus().trim().length()>0&&!materialChukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialChukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}
		return super.findDouble(hql.toString());
	}

	@Override
	public boolean examinAll(MaterialChukuQueryVo materialChukuQueryVo, Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialChuku m set status=1 where status=0  ");
		if(materialChukuQueryVo!=null){
			if(materialChukuQueryVo.getDrawingnum()!=null&&materialChukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialChukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialChukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialChukuQueryVo.getFunitsId()+")");
			}
			if(materialChukuQueryVo.getMaterialCode()!=null&&materialChukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialChukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialChukuQueryVo.getMaterialName()!=null&&materialChukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialChukuQueryVo.getMaterialName()+"%'");
			}
			if(materialChukuQueryVo.getCkBeginDate()!=null&&materialChukuQueryVo.getCkBeginDate().trim().length()>0){
				hql.append(" and ckTime >= TO_DATE('"+materialChukuQueryVo.getCkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getCkEndDate()!=null&&materialChukuQueryVo.getCkEndDate().trim().length()>0){
				hql.append(" and ckTime <= TO_DATE('"+materialChukuQueryVo.getCkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getSpecifications()!=null&&materialChukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialChukuQueryVo.getSpecifications()+"%'");
			}
			if(materialChukuQueryVo.getTexture()!=null&&materialChukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialChukuQueryVo.getTexture()+"%'");
			}
			if(materialChukuQueryVo.getCostType()!=null&&materialChukuQueryVo.getCostType().trim().length()>0){
				hql.append(" and costType = '"+materialChukuQueryVo.getCostType()+"'");
			}
			if(materialChukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialChukuQueryVo.getUnitsId());
			}
			if(materialChukuQueryVo.getCreateBeginTime()!=null&&materialChukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialChukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getCreateEndTime()!=null&&materialChukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialChukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getStatus()!=null&&materialChukuQueryVo.getStatus().trim().length()>0&&!materialChukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialChukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}
		hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialChukuQueryVo.getFunitsId()+")");
		super.executeUpdate(hql.toString());
		return true;
	}
	@Override
	public boolean huituiAll(MaterialChukuQueryVo materialChukuQueryVo, Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialChuku m set status=2 where status=0  ");
		if(materialChukuQueryVo!=null){
			if(materialChukuQueryVo.getDrawingnum()!=null&&materialChukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialChukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialChukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialChukuQueryVo.getFunitsId()+")");
			}
			if(materialChukuQueryVo.getMaterialCode()!=null&&materialChukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialChukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialChukuQueryVo.getMaterialName()!=null&&materialChukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialChukuQueryVo.getMaterialName()+"%'");
			}
			if(materialChukuQueryVo.getCkBeginDate()!=null&&materialChukuQueryVo.getCkBeginDate().trim().length()>0){
				hql.append(" and ckTime >= TO_DATE('"+materialChukuQueryVo.getCkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getCkEndDate()!=null&&materialChukuQueryVo.getCkEndDate().trim().length()>0){
				hql.append(" and ckTime <= TO_DATE('"+materialChukuQueryVo.getCkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getSpecifications()!=null&&materialChukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialChukuQueryVo.getSpecifications()+"%'");
			}
			if(materialChukuQueryVo.getTexture()!=null&&materialChukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialChukuQueryVo.getTexture()+"%'");
			}
			if(materialChukuQueryVo.getCostType()!=null&&materialChukuQueryVo.getCostType().trim().length()>0){
				hql.append(" and costType = '"+materialChukuQueryVo.getCostType()+"'");
			}
			if(materialChukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialChukuQueryVo.getUnitsId());
			}
			if(materialChukuQueryVo.getCreateBeginTime()!=null&&materialChukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialChukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getCreateEndTime()!=null&&materialChukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialChukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getStatus()!=null&&materialChukuQueryVo.getStatus().trim().length()>0&&!materialChukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialChukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}
		hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+units.getUnitsId()+")");
		super.executeUpdate(hql.toString());
		return true;
	}
	@Override
	public boolean shanchuAll(MaterialChukuQueryVo materialChukuQueryVo, Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("delete from  MaterialChuku m  where (status=0 or status=2) ");
		if(materialChukuQueryVo!=null){
			if(materialChukuQueryVo.getDrawingnum()!=null&&materialChukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialChukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialChukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialChukuQueryVo.getFunitsId()+")");
			}
			if(materialChukuQueryVo.getMaterialCode()!=null&&materialChukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialChukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialChukuQueryVo.getMaterialName()!=null&&materialChukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialChukuQueryVo.getMaterialName()+"%'");
			}
			if(materialChukuQueryVo.getCkBeginDate()!=null&&materialChukuQueryVo.getCkBeginDate().trim().length()>0){
				hql.append(" and ckTime >= TO_DATE('"+materialChukuQueryVo.getCkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getCkEndDate()!=null&&materialChukuQueryVo.getCkEndDate().trim().length()>0){
				hql.append(" and ckTime <= TO_DATE('"+materialChukuQueryVo.getCkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getSpecifications()!=null&&materialChukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialChukuQueryVo.getSpecifications()+"%'");
			}
			if(materialChukuQueryVo.getTexture()!=null&&materialChukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialChukuQueryVo.getTexture()+"%'");
			}
			if(materialChukuQueryVo.getCostType()!=null&&materialChukuQueryVo.getCostType().trim().length()>0){
				hql.append(" and costType = '"+materialChukuQueryVo.getCostType()+"'");
			}
			if(materialChukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialChukuQueryVo.getUnitsId());
			}
			if(materialChukuQueryVo.getCreateBeginTime()!=null&&materialChukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialChukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getCreateEndTime()!=null&&materialChukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialChukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getStatus()!=null&&materialChukuQueryVo.getStatus().trim().length()>0&&!materialChukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialChukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}else{
		hql.append("and m.unitsId="+units.getUnitsId());
		}
		super.executeUpdate(hql.toString());
		return true;
	}
	@Override
	public boolean allBack(MaterialChukuQueryVo materialChukuQueryVo, Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialChuku m set status=0 where status=1  ");
		if(materialChukuQueryVo!=null){
			if(materialChukuQueryVo.getDrawingnum()!=null&&materialChukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialChukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialChukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialChukuQueryVo.getFunitsId()+")");
			}
			if(materialChukuQueryVo.getMaterialCode()!=null&&materialChukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialChukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialChukuQueryVo.getMaterialName()!=null&&materialChukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialChukuQueryVo.getMaterialName()+"%'");
			}
			if(materialChukuQueryVo.getCkBeginDate()!=null&&materialChukuQueryVo.getCkBeginDate().trim().length()>0){
				hql.append(" and ckTime >= TO_DATE('"+materialChukuQueryVo.getCkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getCkEndDate()!=null&&materialChukuQueryVo.getCkEndDate().trim().length()>0){
				hql.append(" and ckTime <= TO_DATE('"+materialChukuQueryVo.getCkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialChukuQueryVo.getSpecifications()!=null&&materialChukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialChukuQueryVo.getSpecifications()+"%'");
			}
			if(materialChukuQueryVo.getTexture()!=null&&materialChukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialChukuQueryVo.getTexture()+"%'");
			}
			if(materialChukuQueryVo.getCostType()!=null&&materialChukuQueryVo.getCostType().trim().length()>0){
				hql.append(" and costType = '"+materialChukuQueryVo.getCostType()+"'");
			}
			if(materialChukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialChukuQueryVo.getUnitsId());
			}
			if(materialChukuQueryVo.getCreateBeginTime()!=null&&materialChukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialChukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getCreateEndTime()!=null&&materialChukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialChukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialChukuQueryVo.getStatus()!=null&&materialChukuQueryVo.getStatus().trim().length()>0&&!materialChukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialChukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}else{
			hql.append(" and unitsId="+units.getUnitsId());
		}
		super.executeUpdate(hql.toString());
		return true;
	}
}
